package com.example.demoforscrollconflict

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class ScrollConflictDemoActivity : AppCompatActivity() {
    
    private lateinit var viewPager: ViewPager2
    private lateinit var tabLayout: TabLayout
    private lateinit var recyclerView: RecyclerView
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scroll_conflict_demo)
        
        initViews()
        setupViewPager()
        setupRecyclerView()
    }
    
    private fun initViews() {
        viewPager = findViewById(R.id.viewPager)
        tabLayout = findViewById(R.id.tabLayout)
        recyclerView = findViewById(R.id.recyclerView)
    }
    
    private fun setupViewPager() {
        val adapter = ScrollConflictPagerAdapter()
        viewPager.adapter = adapter
        
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            tab.text = when (position) {
                0 -> "嵌套滚动"
                1 -> "水平冲突"
                2 -> "垂直冲突"
                3 -> "自定义解决"
                else -> "案例${position + 1}"
            }
        }.attach()
    }
    
    private fun setupRecyclerView() {
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = ScrollConflictListAdapter(getMockData())
    }
    
    private fun getMockData(): List<ScrollConflictCase> {
        return listOf(
            ScrollConflictCase(
                "1. 嵌套RecyclerView滑动冲突",
                "当外层RecyclerView包含内层RecyclerView时，需要处理垂直滑动冲突",
                "解决方案：重写onInterceptTouchEvent，判断滑动方向"
            ),
            ScrollConflictCase(
                "2. ViewPager2与RecyclerView水平冲突",
                "ViewPager2的左右滑动与RecyclerView的水平滑动产生冲突",
                "解决方案：使用NestedScrollingParent3接口处理嵌套滚动"
            ),
            ScrollConflictCase(
                "3. 下拉刷新与列表滚动冲突",
                "SwipeRefreshLayout的下拉手势与RecyclerView的滚动冲突",
                "解决方案：重写canChildScrollUp方法，判断列表是否在顶部"
            ),
            ScrollConflictCase(
                "4. 地图与列表混合滚动",
                "地图组件的手势与列表滚动产生冲突",
                "解决方案：使用GestureDetector区分手势类型"
            ),
            ScrollConflictCase(
                "5. 自定义ViewGroup滑动冲突",
                "自定义ViewGroup需要处理子View的滑动事件",
                "解决方案：重写onInterceptTouchEvent和onTouchEvent"
            ),
            ScrollConflictCase(
                "6. 多指手势冲突处理",
                "多个手指同时操作时的滑动冲突",
                "解决方案：使用MotionEvent.getPointerCount()判断触摸点数量"
            ),
            ScrollConflictCase(
                "7. 惯性滚动冲突",
                "快速滑动后的惯性滚动与新的触摸事件冲突",
                "解决方案：使用Scroller处理惯性滚动，及时停止"
            ),
            ScrollConflictCase(
                "8. 嵌套滚动优化",
                "使用Android原生嵌套滚动机制优化性能",
                "解决方案：实现NestedScrollingChild3和NestedScrollingParent3"
            )
        )
    }
}

// 滑动冲突案例数据类
data class ScrollConflictCase(
    val title: String,
    val description: String,
    val solution: String
)

// ViewPager2适配器
class ScrollConflictPagerAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<ScrollConflictPagerAdapter.PagerViewHolder>() {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_scroll_conflict_page, parent, false)
        return PagerViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: PagerViewHolder, position: Int) {
        holder.bind(position)
    }
    
    override fun getItemCount(): Int = 4
    
    class PagerViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        private val titleText: TextView = itemView.findViewById(R.id.pageTitle)
        private val contentText: TextView = itemView.findViewById(R.id.pageContent)
        
        fun bind(position: Int) {
            when (position) {
                0 -> {
                    titleText.text = "嵌套滚动冲突"
                    contentText.text = "这是最常见的滑动冲突场景，当父容器和子容器都需要处理滑动事件时，需要合理分配滑动权限。"
                }
                1 -> {
                    titleText.text = "水平滑动冲突"
                    contentText.text = "ViewPager2的左右滑动与RecyclerView的水平滑动冲突，需要判断用户意图来决定滑动方向。"
                }
                2 -> {
                    titleText.text = "垂直滑动冲突"
                    contentText.text = "下拉刷新、嵌套列表等垂直滑动冲突，需要根据滑动距离和速度来判断是否拦截事件。"
                }
                3 -> {
                    titleText.text = "自定义解决方案"
                    contentText.text = "针对特定业务场景，可以自定义滑动冲突处理逻辑，提供更好的用户体验。"
                }
            }
        }
    }
}

// RecyclerView适配器
class ScrollConflictListAdapter(private val cases: List<ScrollConflictCase>) : 
    androidx.recyclerview.widget.RecyclerView.Adapter<ScrollConflictListAdapter.CaseViewHolder>() {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CaseViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_scroll_conflict_case, parent, false)
        return CaseViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: CaseViewHolder, position: Int) {
        holder.bind(cases[position])
    }
    
    override fun getItemCount(): Int = cases.size
    
    class CaseViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        private val titleText: TextView = itemView.findViewById(R.id.caseTitle)
        private val descText: TextView = itemView.findViewById(R.id.caseDescription)
        private val solutionText: TextView = itemView.findViewById(R.id.caseSolution)
        
        fun bind(case: ScrollConflictCase) {
            titleText.text = case.title
            descText.text = case.description
            solutionText.text = case.solution
        }
    }
}
